<?php

namespace app\rental\taglib;

use think\template\TagLib;

class Rental extends TagLib
{
    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'cars'         => ['attr' => 'field,where,limit,order,page,relation,returnVarName,pageVarName,categoryIds', 'close' => 1],//非必须属性item
        'categories'       => ['attr' => 'where,order', 'close' => 1],//非必须属性item
        'footernavs'       => ['attr' => 'where,order', 'close' => 1],//非必须属性item
    ];

    /**
     * 汽车列表标签
     */
    public function tagCars($tag, $content)
    {
        $item          = empty($tag['item']) ? 'vo' : $tag['item'];//循环变量名
        $field         = empty($tag['field']) ? '' : $tag['field'];
        $order         = empty($tag['order']) ? 'update_time DESC' : $tag['order'];
        $relation   = empty($tag['relation']) ? '' : $tag['relation'];
        $pageVarName   = empty($tag['pageVarName']) ? '__PAGE_VAR_NAME__' : $tag['pageVarName'];
        $returnVarName = empty($tag['returnVarName']) ? 'cars_data' : $tag['returnVarName'];

        $where = '""';
        if (!empty($tag['where']) && strpos($tag['where'], '$') === 0) {
            $where = $tag['where'];
        }

        $limit = "''";
        if (!empty($tag['limit'])) {
            if (strpos($tag['limit'], '$') === 0) {
                $limit = $tag['limit'];
                $this->autoBuildVar($limit);
            } else {
                $limit = "'{$tag['limit']}'";
            }
        }

        $page = "''";
        if (!empty($tag['page'])) {
            if (strpos($tag['page'], '$') === 0) {
                $page = $tag['page'];
            } else {
                $page = intval($tag['page']);
                $page = "'{$page}'";
            }
        }

        $categoryIds = "''";
        if (!empty($tag['categoryIds'])) {
            if (strpos($tag['categoryIds'], '$') === 0) {
                $categoryIds = $tag['categoryIds'];
                $this->autoBuildVar($categoryIds);
            } else {
                $categoryIds = "'{$tag['categoryIds']}'";
            }
        }
        $parse = <<<parse
<?php
\${$returnVarName} = \app\Rental\service\ApiService::cars([
    'field'   => '{$field}',
    'where'   => {$where},
    'limit'   => {$limit},
    'order'   => '{$order}',
    'page'    => $page,
    'relation'=> '{$relation}',
    'category_ids'=>{$categoryIds}
]);

\${$pageVarName} = isset(\${$returnVarName}['page'])?\${$returnVarName}['page']:'';

 ?>
<volist name="{$returnVarName}.cars" id="{$item}">
{$content}
</volist>
parse;
        return $parse;
    }

    /**
     * 汽车分类标签
     */
    public function tagCategories($tag, $content)
    {
        $item          = empty($tag['item']) ? 'vo' : $tag['item'];//循环变量名
        $order         = empty($tag['order']) ? '' : $tag['order'];
        $returnVarName = 'rental_categories_data';
        $where         = '""';
        if (!empty($tag['where']) && strpos($tag['where'], '$') === 0) {
            $where = $tag['where'];
        }

        $parse = <<<parse
<?php
\${$returnVarName} = \app\Rental\service\ApiService::categories([
    'where'   => {$where},
    'order'   => '{$order}',
]);

 ?>
<volist name="{$returnVarName}" id="{$item}">
{$content}
</volist>
parse;
        return $parse;
    }
    /**
     * 底部导航
     */
    public function tagFooternavs($tag, $content)
    {
        $item          = empty($tag['item']) ? 'vo' : $tag['item'];//循环变量名
        $returnVarName = 'rental_footernavs_data';
        $navId = "''";
        if (!empty($tag['navId'])) {
            if (strpos($tag['navId'], '$') === 0) {
                $navId = $tag['navId'];

                $this->autoBuildVar($navId);
            } else {
                $navId = "'{$tag['navId']}'";
            }
        }
        $parse = <<<parse
<?php
\${$returnVarName} = \app\Rental\service\ApiService::footernavs({$navId});

 ?>
<volist name="{$returnVarName}" id="{$item}">
{$content}
</volist>
parse;
        return $parse;
    }
}
